$(document).ready(function() { 
	
	setDatePicker('frmEditProfile_dob');

	// Load Image
	setImage('frmEditProfile_part2_profile_picture', PROFILE_PICTURE);

	// Validate Part1
	$("#frmEditProfile_part1").validate({
		rules: {
			"customerInfo.emailAddress": {
				required: true,
				email: true
			},
			"customerInfo.dob": {
				required: true,
				date: true
			}
		},
		messages: {
			"customerInfo.emailAddress": {
				required: "Email is required",
				email:  "Please enter a valid email address"
			},
			"customerInfo.dob": {
				required: "Birth of date is required",
				date: "Birth of date is invalid"
			}
		},
		submitHandler : function(form) {
			SubmittingForm1();
		},
	});	

	// Update Part2 Start
	$("#frmEditProfile_part2_submit").click(function() {
		var options = {
			beforeSubmit : function(formData, jqForm, options) { // pre-submit
			},
			success : function(responseText, statusText) {
				alert('update successfully!');
			},
			error : function(xhr, ajaxOptions, thrownError) {
				alert(xhr.status);
				alert(thrownError);
			},
			url : ACCOUNT_UPDATE_PROFILE_URL,
			type : 'post', // 'get' or 'post', override for form's 'method'
			dataType : 'json' // 'xml', 'script', or 'json' (expected server
		};
		$('#frmEditProfile_part2').ajaxSubmit(options);
		return false;
	});
	// Update Part2 End

	
	// Validate Part3
	$("#frmEditProfile_part3").validate({
		rules: {
			"customerInfo.password": {
				required: true,
				minlength: 6
			},
			"customerInfo.newPassword": {
				required: true,
				minlength: 6
			},
			"customerInfo.rePassword": {
				required: true,
				minlength: 6,
				equalTo: "#frmEditProfile_part3_newPassword"
			}
		},
		messages: {
			"customerInfo.password": {
				required: "Password is required",
				minlength: "Your password must be at least 6 characters long"
			},
			"customerInfo.newPassword": {
				required: "New password is required",
				minlength: "Your new password must be at least 6 characters long"
			},
			"customerInfo.rePassword": {
				required: "Re-Password is required",
				minlength: "Your re-password must be at least 6 characters long",
				equalTo: "Please enter the same password as above"
			}
		},
		submitHandler : function(form) {
			SubmittingForm3();
		},
	});	
	
	// Subscribe
	$("#frmEditProfile_subscribe_submit").click(function() {
			var options = {
				beforeSubmit : function(formData, jqForm, options) { // pre-submit
				},
				success : function(responseText, statusText) {
					alert('subscribe successfully!');
				},
				error : function(xhr, ajaxOptions, thrownError) {
					alert(xhr.status);
					alert(thrownError);
				},
				url : ACCOUNT_SUBSCRIBE_URL,
				type : 'post', // 'get' or 'post', override for form's 'method'
				dataType : 'json' // 'xml', 'script', or 'json' (expected server
			};
			$('#frmEditProfile_subscribe').ajaxSubmit(options);
			return false;
		});
	
}); 


// Update Part1 Start
//$("#frmEditProfile_part1_submit").click(function() {
function SubmittingForm1() {
	var options = {
		beforeSubmit : function(formData, jqForm, options) { // pre-submit
		},
		success : function(responseText, statusText) {
			if (responseText != null && responseText.errorMessage != null && responseText.errorMessage != '') {
				alert(responseText.errorMessage);
			} else {
				alert('update successfully!');
			}
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert(xhr.status);
			alert(thrownError);
		},
		url : ACCOUNT_UPDATE_PROFILE_URL,
		type : 'post', // 'get' or 'post', override for form's 'method'
		dataType : 'json' // 'xml', 'script', or 'json' (expected server
	};
	$('#frmEditProfile_part1').ajaxSubmit(options);
	return false;
}
// Update Part1 End

// Update Part3 Start
function SubmittingForm3() {
	var options = {
		beforeSubmit : function(formData, jqForm, options) { // pre-submit
		},
		success : function(responseText, statusText) {
			alert('update successfully!');
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert(xhr.status);
			alert(thrownError);
		},
		url : ACCOUNT_UPDATE_PASSWORD_URL,
		type : 'post', // 'get' or 'post', override for form's 'method'
		dataType : 'json' // 'xml', 'script', or 'json' (expected server
	};
	$('#frmEditProfile_part3').ajaxSubmit(options);
	return false;
}
// Update Part3 End


function _uploadSuccess(responseText, statusText) {
	try {
		if(responseText !== null) {
			var strResource = '';
			if(responseText.resource !== null) {
				strResource = responseText.resource.uri;
			}
			$("#frmEditProfile_part2_imageUri_hidden").val(strResource);  // Hidden
			setImage("frmEditProfile_part2_profile_picture", strResource);// Image
		}						
	} catch (e) {
		alert(e);
	}
}	
